<?php

/* Page controller class */
class edit_smiley_handler extends _site_handler {
	public $Smiley;
	public $Error;
	public $Message;

	public $SmileyId;
	public $SmileyTitle;
	public $SmileyDescription;
	public $View;

	// Render page
	public function __construct($m, $view) {
		$this->InitializeSite($m);
		$this->View = $view;
		$this->SmileyId = @$_GET['smiley_id'];

		$this->Wordlets->AddWordlets('edit_smiley');

		$this->Title = 'Edit Smiley';

		$this->Initialize();
		$this->RenderView();
	}

	public function Initialize() {
		if ( !($this->SmileyId = intval($this->SmileyId)) || !($this->Smiley = new Smiley($this->SmileyId)) ) {
			$this->Error = 'Smiley does not exist';
			return;			
		}

		if ( !$this->User || $this->Smiley->User()->Id() != $this->User->Id() ) {
			$this->Error = 'You don\'t own this smiley';
			return;			
		}

		if ( $this->Smiley->Title() ) $this->Title .= ': ' . $this->Smiley->Title();

		$updates = array();

		if ( isset($_POST['title']) ) {
			$updates['title'] = $this->SmileyTitle = $_POST['title'];
		} else {
			$this->SmileyTitle = $this->Smiley->Title();
		}

		if ( isset($_POST['description']) ) {
			$updates['description'] = $this->SmileyDescription = $_POST['description'];
		} else {
			$this->SmileyDescription = $this->Smiley->Description(false);
		}

		if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
			$this->Smiley->Edit($updates);
		}
	}

	public function RenderView() {
		$this->RenderViewContent($this->View);
	}
}